package com.leyou.trade.web;

import com.leyou.trade.dto.OrderDTO;
import com.leyou.trade.dto.OrderFormDTO;
import com.leyou.trade.service.OrderService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author BaiZY on 2020/9/7.
 * @version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 创建订单
     * @param orderDTO 订单数据
     * @return 订单id
     */
    @PostMapping
    public ResponseEntity<Long> createOrder(@RequestBody OrderFormDTO orderDTO, HttpServletRequest request){

        return ResponseEntity.status(HttpStatus.CREATED).body(orderService.createOrder(orderDTO,request));
    }

    /**
     * 根据id查询订单
     * @param orderId 订单id
     * @return 订单对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<OrderDTO> queryOrderById(@PathVariable("id") Long orderId){

        return ResponseEntity.ok(new OrderDTO(orderService.getById(orderId)));
    }

    /**
     * 查询订单支付状态
     * @param orderId 订单id
     * @return 状态值
     */
    @GetMapping("/status/{id}")
    public ResponseEntity<Integer> queryOrderStatus(@PathVariable("id")Long orderId){
        return ResponseEntity.ok(orderService.queryOrderStatus(orderId));
    }

}
